package jhm;

import static org.junit.Assert.assertTrue;
import jhm.test.RobotTestBed;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * Tests if robot's gun is facing correct direction when the robot 
 * is parallel to the right or left wall.
 * 
 * @author Jarret
 * 
 */
public class TestGunHeading extends RobotTestBed {
  // set to true if robot is parallel to right or left wall
  boolean parallelToWall = false;
  // set to true if robot is close to right or left wall
  boolean closeToWall = false;
  // gets the battle field width
  int battleFieldWidth = battleFieldSpec.getWidth();
  // set to true if gun is facing to left side of where robot is facing
  boolean gunFacingLeft = false;

  /**
   * Specifies that Fire and CounterBot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Fire,jhm.CounterBot";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Tests if robot is moving parallel to the right or left wall every round.
   * Also tests if robot's gun has turned 90 degrees to the left.
   * 
   * @param event The TurnEndedEvent
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // gets a snapshot of both CounterBot each turn
    IRobotSnapshot counterBot = event.getTurnSnapshot().getRobots()[1];
    // gets the heading of CounterBot's gun
    double gunHeading = Math.floor(counterBot.getGunHeading() % 270);
    // robot's x coordinate
    double xPos = counterBot.getX();

    // check if robot is close to right or left wall
    if (battleFieldWidth - 30 < xPos || xPos < 30) {
      closeToWall = true;
      // check if robot is facing 0 degrees or 180 degrees
      if (Math.floor(counterBot.getBodyHeading()) <= 1
          || Math.floor(counterBot.getBodyHeading()) >= 179) {
        parallelToWall = true;
      }
      if (gunHeading == 0) {
        gunFacingLeft = true;
      }
    }
  }

  /**
   * Asserts if robot had its gun facing in correct direction during the battle.
   * 
   * @param event The RoundEndedEvent
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {
    assertTrue("Gun Direction", gunFacingLeft);
    assertTrue("Close to wall", closeToWall);
    assertTrue("Check Parallel", parallelToWall);
  }

}
